<?php
namespace app\app\controller\v1;
use think\Db;
use app\app\util\OnlineUtils;
class  MatchRoomController extends BaseController {


    /**
     * @return array
     * [ 匹配模式 创建房间 ]
     */
    public function room()
    {
        //ft_match_room表插入数据，创建房间
        $user_id =  OnlineUtils::getUserIdBySid();           //邀请人id
        $match_room['user_id'] = $user_id;
        $match_room['created_at'] = time();
        $id = DB::name('match_room')->insertGetId($match_room);  //房间id
        return ft(200,'success',['user_id'=>$user_id,'room_id'=>$id]);
    }


    /**
     * @return array
     * [ 创建房间，邀请好友 ]
     */
    public function invitedFriend()
    {
        $match_room_id = input('post.match_room_id');
        $master_user_id = input('post.user_id');
        $user_id = OnlineUtils::getUserIdBySid();
        $master_user = Db::name('user')->find($master_user_id);
        $user = Db::name('user')->find($user_id);
        return wc(200,'success',['room_id'=>$match_room_id,'master_user'=>$master_user,'user'=>$user]);
    }
    /**
     * @return array
     * [ 游戏对战信息 ]
     */
    public function matchFight()
    {
        $shoot_score = input('post.shoot_score');                          //射门分
        $gate_score = input('post.gate_score');                            //守门分
        //邀请人信息
        $master['user_id'] = input('post.user_id')?input('post.user_id'):0;                                   //邀请的人
        $master['match_room_id'] = input('post.match_room_id')?input('post.match_room_id'):0; //房间号
        $master['role_id'] = input('post.role_id')?input('post.role_id'):0;                    //邀请人角色id
        $master['position'] = input('post.position')?input('post.position'):0;                 //邀请人位置
        $master['prop_id'] = input('post.prop_id')?input('post.prop_id'):0;                    //邀请人道具id
        $master['score'] = $shoot_score .':'. $gate_score ;
        $master['created_at'] = time();
        //被邀请人信息
        $guest['user_id'] = OnlineUtils::getUserIdBySid();                                   //被邀请的人
        $guest['match_room_id'] = input('post.match_room_id')?input('post.match_room_id'):0; //房间号
        $guest['role_id'] = input('post.role_id')?input('post.role_id'):0;                    //被邀请人角色id
        $guest['position'] = input('post.position')?input('post.position'):0;                 //被邀请人位置
        $guest['prop_id'] = input('post.prop_id')?input('post.prop_id'):0;                    //被邀请人道具id
        $guest['score'] = $shoot_score .':'. $gate_score ;
        $guest['created_at'] = time();
        //不用位置 不同分数
        if( $guest['position'] == 1 ){           //被邀请人射门
            if ( $shoot_score == 3 ){
                $guest['result'] = 1;            //被邀请人获胜
                $guest['integral'] = 5;          //被邀请人加的积分是5
                $master['result'] = 2;           //邀请人失败
                $master['integral'] = 1;         //邀请人加的积分是1
            }elseif ( $gate_score == 3 ){
                $guest['result'] = 2;            //被邀请人失败
                $guest['integral'] = 1;          //被邀请人加的积分是1
                $master['result'] = 1;           //邀请人获胜
                $master['integral'] = 5;         //邀请人加的积分是5
            }
        }elseif ( $guest['position'] == 0 ){     //被邀请人是守门
            if ( $gate_score == 3 ){
                $guest['result'] = 1;            //被邀请人获胜
                $guest['integral'] = 5;          //被邀请人加的积分是5
                $master['result'] = 2;           //邀请人失败
                $master['integral'] = 1;         //邀请人加的积分是1
            }elseif ( $shoot_score == 3 ){
                $guest['result'] = 2;            //被邀请人失败
                $guest['integral'] = 1;          //被邀请人加的积分是1
                $master['result'] = 1;           //邀请人获胜
                $master['integral'] = 5;         //邀请人加的积分是5
            }
        }

    }

    //人机对战返回假数据
    public function ManMachine(){
        $id   = rand(1,12);
        $data = Db::name('card')->field('id,name,concat("'.config('IMG_HEAD').'",img) avatar_url
        ')->where('id','eq',$id)->find();
        return ft(200,'success',['data'=>$data]);
    }

}